def compare(fname1,fname2):
    f1 = open(fname1,encoding='utf-8')
    f2 = open(fname2,encoding='utf-8')

    s1 = f1.read() 
    s2 = f2.read()

    m1 = len(s1)
    m2 = len(s2) 

    if m1<m2:
        m=m1
    else:
        m=m2

    i=0
    n_line=1
    n_col=0



    while i<m:
        if s1[i]=='\n':
            n_line+=1
            n_col=0

        n_col+=1    

        if s1[i]==s2[i]:
            i+=1
            continue 
        else:
            print("行：",n_line,"列:",n_col,"有差异",sep='')
            break
        
    else:
        if m1==m2:
            print("文件内容相同") 
        else:
            print("行：",n_line,"列:",n_col+1,"有差异",sep='') 


compare(r'E:\code\作业\文件\file1.txt',r'E:\code\作业\文件\file3.txt')  



